﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common.Entity;
using ESOL.Business.Prod;
using ESOL.Business.Sales;
using ESOL.Business.Mst;
using ESOL.Business.Proj;
using ESOL.Business.Inv;

namespace ESOL.WinUI.Manufacture
{
    public partial class frmMaintainAssemblePicking : BaseForm.frmBaseDialogyMaintainForm
    {
        #region vars
        private ProdPlanOrderItemEntity mSelectedPlanOrderItem;
        private InvDispatchOrderEntity mSelectedDispatchOrder;
        private List<InvDispatchItemEntity> listSlectedDispatchItem;
        private bool IsCompleted = false;
        #endregion

        #region ctors
        public frmMaintainAssemblePicking(ProdPlanOrderItemEntity entity)
        {
            InitializeComponent();
            mSelectedPlanOrderItem = entity;
        }
        #endregion

        #region functions
        private void SetField()
        {
            if (mSelectedPlanOrderItem == null)
            {
                return;
            }
            ProdPlanOrderEntity mPlanOrder = ProdPlanOrderBs.GetProdPlanOrderByID(mSelectedPlanOrderItem.PlanOrderId);
            SalesProdPlanApplicationItemEntity mApplicationItem = SalesProdPlanApplicationItemBs.GetProdPlanApplicationItemByID(mPlanOrder.ApplicationItemId);
            if (mApplicationItem == null)
            {
                MessageBox.Show("工令单无法找到对应计划申请单，请联系管理员！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtWorkingOrder.Focus();
                return;
            }
            if (mApplicationItem.ProductId.Equals(mSelectedPlanOrderItem.ProductId))
            {
                IsCompleted=true;
            }
            MstProductEntity mProduct = MstProductBs.GetMstProductTypeByID(mSelectedPlanOrderItem.ProductId);
            if (mProduct == null)
            {
                return;
            }
            txtWorkingOrder.Text = mSelectedPlanOrderItem.OrderItemCode;
            txtProductName.Text = mProduct.ProductName;
            txtProductCode.Text = mProduct.ProductCode;
            txtPickingQty.Text = mSelectedPlanOrderItem.Qty.ToString();
            txtContractModel.Text = mApplicationItem.ProductSpecOut;
        }

        private void BindGrid()
        {
            if (mSelectedPlanOrderItem == null)
                return;
            //string sBlueprintID = mSelectedPlanOrderItem.BlueprintId;
            //DataTable dt = ProjMBOMBs.GetDtProductBomByBomID(mSelectedPlanOrderItem.BomId, "PDTYPE01", "");
            DataTable dt = ProjMBOMBs.GetDtProductBomByParentBomID( mSelectedPlanOrderItem.BomId);
            //dt.Rows.RemoveAt(0);
            //DataTable dt = BptBlueprintBs.GetProductComponentByBlueprintID(sBlueprintID);
            if (dt.Rows.Count==0)
            {
                MessageBox.Show("该产品不存在物料清单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtWorkingOrder.Focus();
                return;
            }
            dt.Columns.Add("PICKING_QTY", typeof(decimal));
            foreach (DataRow dr in dt.Rows)
            {
                dr["PICKING_QTY"] = mSelectedPlanOrderItem.Qty * Convert.ToDecimal(dr["UNIT_DOSE"].ToString());
            }
            gvMain.DataSource = dt;
        }

        private void SetEntity()
        {
            mSelectedDispatchOrder = new InvDispatchOrderEntity();
            mSelectedDispatchOrder.DispatchOrderId = Guid.NewGuid().ToString();
            if (IsCompleted)
            {
                mSelectedDispatchOrder.OrderType = "IDT07";
            }
            else
            {
                mSelectedDispatchOrder.OrderType = "IDT08";
            }
            mSelectedDispatchOrder.OrderDate = DateTime.Now;
            mSelectedDispatchOrder.OrderPerson = Common.CommonParameter.CurrentUser.PersonId;
            mSelectedDispatchOrder.RelatedOrder = mSelectedPlanOrderItem.PlanOrderItemId;
            mSelectedDispatchOrder.Remark = "装配领料，系统自动生成！";
            mSelectedDispatchOrder.CreatedDate = DateTime.Now;
            mSelectedDispatchOrder.CreatedBy = Common.CommonParameter.CurrentUser.PersonId;
            mSelectedDispatchOrder.LastModifiedDate = DateTime.Now;
            mSelectedDispatchOrder.LastModifiedBy = Common.CommonParameter.CurrentUser.PersonId;
            mSelectedDispatchOrder.Status = "IDO01";

            listSlectedDispatchItem=new List<InvDispatchItemEntity>();
            DataTable dt = (DataTable)gvMain.DataSource;
            foreach (DataRow dr in dt.Rows)
            {
                InvDispatchItemEntity m = new InvDispatchItemEntity();
                m.DispatchItemId = Guid.NewGuid().ToString();
                m.DispatchOrderId = mSelectedDispatchOrder.DispatchOrderId;
                m.ProductId = dr["PRODUCT_ID"].ToString();
                m.Qty=Convert.ToDecimal(dr["PICKING_QTY"]);
                listSlectedDispatchItem.Add(m);
            }
        }

        private bool MakeInvDispatchOrder()
        {
            SetEntity();
            if (InvDispatchOrderBs.MakeInvDipatchOrder(mSelectedDispatchOrder, listSlectedDispatchItem, mSelectedPlanOrderItem))
            {
                MessageBox.Show("领料申请成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return true;
            }
            else
            {
                MessageBox.Show("领料申请失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            SetField();
            BindGrid();
        }

        private void txtWorkingOrder_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainWorkingOrderQuery frm = new frmMaintainWorkingOrderQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedProdPlanOrderItem != null)
            {
                mSelectedPlanOrderItem = frm.SelectedProdPlanOrderItem;
                txtWorkingOrder.Text = mSelectedPlanOrderItem.OrderItemCode;
            }
            else
            {
                mSelectedPlanOrderItem = null;
                txtWorkingOrder.Text = "";
            }
            SetField();
            BindGrid();
        }

        private void txtWorkingOrder_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter)
                return;
            if (string.IsNullOrEmpty(txtWorkingOrder.Text))
            {
                MessageBox.Show("工令单不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtWorkingOrder.Focus();
                return;
            }
            mSelectedPlanOrderItem = null;
            mSelectedPlanOrderItem = ProdPlanOrderItemBs.GetProdPlanOrderItemByCode(txtWorkingOrder.Text);
            if (mSelectedPlanOrderItem == null)
            {
                MessageBox.Show("工令单不存在！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtWorkingOrder.Focus();
                return;
            }
            SetField();
            BindGrid();
        }

        private void txtPickingQty_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter)
            {
                return;
            }
            if (string.IsNullOrEmpty(txtPickingQty.Text))
            {
                MessageBox.Show("领料数量不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPickingQty.Focus();
                return;
            }
            decimal dPickingQty = Convert.ToDecimal(txtPickingQty.Text);
            DataTable dt = (DataTable)gvMain.DataSource;
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    dr["PICKING_QTY"] = Convert.ToDecimal(dr["MATERIAL_QTY"].ToString()) * dPickingQty;
                }
            }

        }

        protected override void Click_Save()
        {
            if (MakeInvDispatchOrder())
            {
                DialogResult = DialogResult.OK;
            }
        }
        #endregion
    }
}
